package com.guoranhao.web.resolver;

import com.guoranhao.tool.utils.ToolsUtil;
import com.guoranhao.web.view.JaxbJsonView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: 根据异常类型，处理返回结果
 * All Rights Reserved.
 * @version 1.0  2016年6月17日 下午3:25:39  by 代鹏（daipeng.456@gmail.com）创建
 */
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
	
	private static Logger logger = LoggerFactory.getLogger(ExceptionResolver.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		logger.info(String.format("Exception Message is:%s, Cause is:%s", ex.getMessage(), ex.getCause()));
//		if (ex instanceof UserNotExistException) {
//			return new ModelAndView(new RedirectView());
//		} else if (ex instanceof PermissionDeniedException) {
//			ModelAndView mv = new ModelAndView("/404");
//			mv.addObject("commonVO", new Object());
//			return mv;
//		}
		return new ModelAndView(new JaxbJsonView(ToolsUtil.builderMapParams("result", "failed")));
	}
}
